package com.liyu.listener;

import com.liyu.config.AlternateExchangeCommon;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * @author 李誉
 * @date 2022-07-24 21:59
 * @description 备份交换机相关队列监听测试
 */
@Component
public class AlternateConsumer {

    @RabbitListener(queues = AlternateExchangeCommon.ALTERNATE_QUEUE)
    public void alterQueueListener(Channel channel, Message message) throws IOException {
        String msg = new String(message.getBody(), StandardCharsets.UTF_8);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        System.out.println("备份队列接收到信息：" + msg);
    }

    @RabbitListener(queues = AlternateExchangeCommon.WARNING_QUEUE)
    public void warnQueueListener(Channel channel, Message message) throws IOException {
        String msg = new String(message.getBody(), StandardCharsets.UTF_8);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        System.out.println("异常队列接收到信息：" + msg);
    }
}
